<template>
  <el-card style="margin-bottom: 10px">
    <el-form
      :inline="true"
      :disabled="isCheckInput"
      class="demo-form-inline mySelect"
    >
      <!-- 一级分类 -->
      <el-form-item label="一级分类">
        <el-select placeholder="请选择" v-model="category1Id">
          <el-option
            v-for="c1 in categoryStore.category1List"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          />
        </el-select>
      </el-form-item>
      <!-- 二级分类 -->
      <el-form-item label="二级分类">
        <el-select placeholder="请选择" v-model="category2Id">
          <el-option
            v-for="c2 in categoryStore.category2List"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          />
        </el-select>
      </el-form-item>
      <!-- 三级分类 -->
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="category3Id">
          <el-option
            v-for="c3 in categoryStore.category3List"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "CategorySelector",
  props: ["isCheckInput"],
}
</script>

<script setup lang="ts">
import { reactive, onMounted, computed } from "vue"
import { useCategoryListStore } from "@/stores/categoryList"

// 创建store对象
const categoryStore = useCategoryListStore()

// 1.在组件挂载时，调用函数，获取一级分类数据
onMounted(() => {
  categoryStore.getCategory1ListData()
})

// 2. 使用计算属性 精简一下category1Id
// 顺便在切换一级导航分类时，立即发送请求 获取二级导航 数据
const category1Id = computed({
  get() {
    return categoryStore.category1Id as number
  },
  set(val: number) {
    categoryStore.getCategory2ListData(val)
  },
})

// 3.使用计算属性 精简一下category2Id
const category2Id = computed({
  get() {
    return categoryStore.category2Id as number
  },
  set(val: number) {
    categoryStore.getCategory3ListData(val)
  },
})

// 4.使用计算属性 精简一下category3Id
const category3Id = computed({
  get() {
    return categoryStore.category3Id as number
  },
  set(val: number) {
    // categoryStore.category3Id = val
    categoryStore.setCategory3Id(val)
  },
})
</script>

<style lang="scss">
.mySelect {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
